package it.koutifaris.travianmap.view.distances;

import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.context.HasApplicationContex;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class DistancesViewImpl extends HasApplicationContex implements DistancesView {

	private final View root;

	public DistancesViewImpl(View root) {
		this.root = root;
	}

	@Override
	public void setDistance(float distance) {
		String renderedDistance =
				getTravianMapContext().getLocaleSettings().getDefaultNumberFormat().format(distance);

		getDistanceView().setText(renderedDistance);
	}

	private TextView getDistanceView() {
		return (TextView) root.findViewById(R.id.distances_space);
	}

	@Override
	public ListView getListView() {
		return (ListView) root.findViewById(R.id.distances_listview);
	}

	@Override
	public Spinner getFromSpinner() {
		return (Spinner) root.findViewById(R.id.distances_from_spinner);
	}

	@Override
	public Spinner getToSpinner() {
		return (Spinner) root.findViewById(R.id.distances_to_spinner);
	}

	@Override
	public DistancesItemView itemViewFor(View itemView) {
		return new DistancesItemViewImpl(itemView);
	}

	@Override
	public View newItemView(ViewGroup parent) {
		LayoutInflater inflater = getInflater();
		return inflater.inflate(R.layout.distances_item, parent, false);
	}

	private LayoutInflater getInflater() {
		return (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

}
